package com.guigu.erp.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ManufactureManufactureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:ManufactureManufactureController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/25 21:36
 * @Author:chenchong
 */
@Controller
@RequestMapping("/ManufactureManufacture")
public class ManufactureManufactureController {

    @Autowired
    private ManufactureManufactureService manufactureManufactureService;
    @Autowired
    private ManufactureProcedureService manufactureProcedureService;
    @Autowired
    private StoragePayService storagePayService;
    @Autowired
    private StoragePayDetailsService storagePayDetailsService;
    @Autowired
    private ManufactureProcedureModuleService manufactureProcedureModuleService;
    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private ManufactureApplyService manufactureApplyService;

    @RequestMapping("/auditShow")
    @ResponseBody
    public List<ManufactureManufacture> auditShow() {
        QueryWrapper<ManufactureManufacture> qw = new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-0");
        List<ManufactureManufacture> list = manufactureManufactureService.list(qw);
        return list;
    }

    @RequestMapping("/getById")
    @ResponseBody
    public ManufactureManufacture getById(int id) {
        ManufactureManufacture list = manufactureManufactureService.getById(id);
        return list;
    }

    @RequestMapping("/getOneProcedure")
    @ResponseBody
    public ManufactureProcedure getOneProcedure(int id) {
        ManufactureProcedure list = manufactureProcedureService.getById(id);
        return list;
    }
    @RequestMapping("/show100")
    @ResponseBody
    public List<ManufactureManufacture> show100() {
        QueryWrapper<ManufactureManufacture> qw=new   QueryWrapper<>();
        qw.eq("CHECK_TAG","S001-1");
        qw.ne("MANUFACTURE_PROCEDURE_TAG","S002-2");
        List<ManufactureManufacture> list=manufactureManufactureService.list(qw);
        return list;
    }

    @RequestMapping("/show101")
    @ResponseBody
    public List<ManufactureManufacture> show101() {
        QueryWrapper<ManufactureManufacture> qw=new   QueryWrapper<>();
        qw.eq("CHECK_TAG","S001-1");
        qw.eq("MANUFACTURE_PROCEDURE_TAG","S002-1");
        List<ManufactureManufacture> list=manufactureManufactureService.list(qw);
        return list;
    }

    @RequestMapping("/showProcedure")
    @ResponseBody
    public List<ManufactureProcedure> showProcedure(int parentId) {

        QueryWrapper<ManufactureProcedure> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", parentId);
        List<ManufactureProcedure> list = manufactureProcedureService.list(qw);
        return list;
    }

    @RequestMapping("/show")
    @ResponseBody
    public PageUtils<ManufactureManufacture> show(ManufactureManufactureVo vo, int pageNo, int pageSize) {
        QueryWrapper<ManufactureManufacture> qw = new QueryWrapper<>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getManufactureId())) {
                qw.eq("MANUFACTURE_ID", vo.getManufactureId());
            }
            if (!StringUtils.isEmpty(vo.getCheckTag())) {
                qw.eq("CHECK_TAG", vo.getCheckTag());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("REGISTER_TIME", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("REGISTER_TIME", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureManufacture> list = manufactureManufactureService.list(qw);
        PageInfo<ManufactureManufacture> p = new PageInfo<>(list);
        PageUtils<ManufactureManufacture> utils = new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @RequestMapping("/ProceduremoduleById")
    @ResponseBody
    public List<ManufactureProcedureModule> ProceduremoduleById(int id) {

        QueryWrapper<ManufactureProcedureModule> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", id);
        List<ManufactureProcedureModule> list = manufactureProcedureModuleService.list(qw);
        return list;
    }

    @RequestMapping("/updateAdd")
    @ResponseBody
    public String updateAdd(ManufactureManufacture vo) {
        String checkTag = vo.getCheckTag();
        if (checkTag.equals("S001-2")) {
            ManufactureManufacture one = manufactureManufactureService.getById(vo.getId());
            String applyIdGroup = one.getApplyIdGroup();


            int w = applyIdGroup.indexOf("/");
            if (w == -1) {
                QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
                qw.eq("APPLY_ID", applyIdGroup);
                ManufactureApply one1 = manufactureApplyService.getOne(qw);
                one1.setManufactureTag("P001-0");
                manufactureApplyService.updateById(one1);
            } else {

                String[] split = applyIdGroup.split("/");
                for (int i = 0; i < split.length; i++) {
                    QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
                    qw.eq("APPLY_ID", split[i]);
                    List<ManufactureApply> list = manufactureApplyService.list(qw);
                    for (int j = 0; j < list.size(); j++) {
                        list.get(j).setManufactureTag("P001-0");
                        manufactureApplyService.updateById(list.get(j));
                    }


                }
            }

            QueryWrapper<ManufactureProcedure> qw1 = new QueryWrapper();
            qw1.eq("PARENT_ID", vo.getId());
            List<ManufactureProcedure> list = manufactureProcedureService.list(qw1);
            for (int i = 0; i < list.size(); i++) {
                ManufactureProcedure manufactureProcedure = list.get(i);
                QueryWrapper<ManufactureProcedureModule> qw2 = new QueryWrapper();
                qw2.eq("PARENT_ID", manufactureProcedure.getId());
                List<ManufactureProcedureModule> list1 = manufactureProcedureModuleService.list(qw2);
                for (int j = 0; j < list1.size(); j++) {

                    manufactureProcedureModuleService.removeById(list1.get(j).getId());
                }
                manufactureProcedureService.removeById(list.get(i).getId());
            }
            manufactureManufactureService.removeById(vo.getId());

        } else {
            QueryWrapper<ManufactureManufacture> qw = new QueryWrapper();
            ManufactureManufacture byId = manufactureManufactureService.getById(vo.getId());
            byId.setCheckTag("S001-1");
            manufactureManufactureService.updateById(byId);
            QueryWrapper<ManufactureProcedure> qw1 = new QueryWrapper();
            qw1.eq("PARENT_ID", vo.getId());
            List<ManufactureProcedure> list = manufactureProcedureService.list(qw1);
            BigDecimal b = new BigDecimal(0);
            BigDecimal b1 = new BigDecimal(0);
            for (int i = 0; i < list.size(); i++) {
                QueryWrapper<ManufactureProcedureModule> qw2 = new QueryWrapper();
                qw2.eq("PARENT_ID", list.get(i).getId());
                List<ManufactureProcedureModule> list1 = manufactureProcedureModuleService.list(qw2);
                for (int j = 0; j < list1.size(); j++) {
                    ManufactureProcedureModule manufactureProcedureModule = list1.get(j);
                    BigDecimal amount = manufactureProcedureModule.getAmount();
                    b = b.add(amount);
                    BigDecimal subtotal = manufactureProcedureModule.getSubtotal();
                    b1 = b1.add(subtotal);
                }
                ManufactureProcedure manufactureProcedure = list.get(i);
                StoragePay stor = new StoragePay();
                String typeNumber = numberUtils.getTypeNumber("402");
                stor.setPayId(typeNumber);
                stor.setReason("C002-1");
                stor.setReasonexact(manufactureProcedure.getProcedureName());
                stor.setAmountSum(b);
                stor.setPaidAmountSum(0);
                stor.setCostPriceSum(b1);
                stor.setRemark(vo.getRemark());
                stor.setRegister(vo.getRegister());
                stor.setRegisterTime(vo.getRegisterTime());
                stor.setChecker(vo.getChecker());
                stor.setCheckTime(vo.getCheckTime());
                stor.setCheckTag("S001-1");
                stor.setStoreTag("K002-1");
                storagePayService.save(stor);
                QueryWrapper<ManufactureProcedureModule> qw3 = new QueryWrapper();
                qw3.eq("PARENT_ID", list.get(i).getId());
                List<ManufactureProcedureModule> list2 = manufactureProcedureModuleService.list(qw2);
                for (int w = 0; w < list2.size(); w++) {
                    ManufactureProcedureModule manufactureProcedureModule2 = list2.get(w);
                    StoragePayDetails s = new StoragePayDetails();
                    s.setParentId(stor.getId());
                    s.setProductId(manufactureProcedureModule2.getProductId());
                    s.setProductName(manufactureProcedureModule2.getProductName());
                    s.setAmount(manufactureProcedureModule2.getAmount());
                    s.setCostPrice(manufactureProcedureModule2.getCostPrice());
                    s.setSubtotal(manufactureProcedureModule2.getSubtotal());
                    s.setPayTag("K002-1");
                    storagePayDetailsService.save(s);
                }
            }
        }
        return "ok";
    }


    @RequestMapping("/getbyId2")
    @ResponseBody
    public ManufactureProcedure getbyId2(int id) {
        ManufactureProcedure byId = manufactureProcedureService.getById(id);
        return byId;
    }

    @RequestMapping("/MPMshowId")
    @ResponseBody
    public List<ManufactureProcedureModule> MPMshowId(int id) {
        QueryWrapper<ManufactureProcedureModule> qw=new QueryWrapper<>();
        qw.eq("PARENT_ID",id);
        List<ManufactureProcedureModule> list = manufactureProcedureModuleService.list(qw);
        return list;
    }
}

